<template>
	<view class="setting">
		<view class="nav-header-box">
			<view class="header-left" @click="goback">
				<view class="header-left-icon"></view>
			</view>
			<view class="header-title">
				<view class="header-p">修改个人信息</view>
			</view>
			<view class="header-right" @click="sevaImgFunc">
				<view class="header-right-icon"></view>
			</view>
		</view>

		<view class="edit-img-box">
			<view class="img-icon" @click="getImage">
				<view class="icon">
				</view>
			</view>

			<image :src="user_avators" mode=""></image>
		</view>

		<view class="edit-info-list">
			<view class="edit-content">
				<view class="edit-title">用户名</view>
				<input type="text" class="edit-input" v-model="username">
			</view>
			<view class="edit-content">
				<view class="edit-title">邮箱</view>
				<input type="text" class="edit-input" v-model="useremail">
			</view>
			<view class="edit-content">
				<view class="edit-title">电话</view>
				<input type="text" class="edit-input" v-model="userphone">
			</view>
			<view class="edit-content">
				<view class="edit-title">个性签名</view>
				<textarea class="edit-massage" v-model="usermassage"></textarea>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getUser,
		upLoadImg,
		saveImg,
		deteleImg
	} from "@/api/request"
	export default {
		data() {
			return {
				user_id: "",
				userInfo: {},
				username: "",
				useremail: "",
				userphone: "",
				usermassage: "",
				user_avators: "",
				old_user_avators: "",
			}
		},
		methods: {
			//返回上一页
			goback() {
				console.log(111);
				uni.navigateBack({
					delta: 1, //返回层数，2则上上页
				})
			},
			//保存修改的用户
			sevaImgFunc() {
				let data = {
					user_id: this.user_id,
					user_nick_name: this.username,
					user_message: this.usermassage,
					user_avators: this.user_avators,
				}
				saveImg(data).then(content => {
					console.log(content);
					let {
						code,
						msg,
						user_avators
					} = content
					if (code == 200) {
						localStorage.setItem("user_avators", user_avators)
						localStorage.setItem("user_message", this.usermassage)
						localStorage.setItem("user_nick_name", this.username)
						this.user_avators = user_avators
						if (localStorage.getItem("userInfo")) {
							let userInfo = JSON.parse(localStorage.getItem("userInfo"));
							userInfo.user_avators = user_avators;
							userInfo.user_message = this.usermassage;
							userInfo.user_nick_name = this.username;
							localStorage.setItem("userInfo", JSON.stringify(userInfo));
						}
						uni.showToast({
							title: "修改信息成功",
							icon: "none"
						})
						// deteleImg({
						// 	user_avator: this.old_user_avators
						// }).then((result) => {
						// 	console.log("删除旧头像:", result);
						// });
					}
				})
			},
			
			//上传头像
			getImage() {
				let that = this;
				uni.chooseImage({
					count: 6, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: function(res) {
						// console.log("111",res.tempFiles[0]);
						let formData = new FormData()
						formData.append("photo", res.tempFiles[0]);
						console.log("11111===>",formData);
						upLoadImg(formData).then(content => {
							console.log(content);
							let {
								code,
								msg,
								picUrl
							} = content
							if (code == 200) {
								that.user_avators = picUrl;
								// that.old_user_avators=picUrl
							}
						})
						// console.log(that);
						// console.log("222",JSON.stringify(res.tempFilePaths));
					}
				});
			},
			//获取用户信息
			getUserInfo() {
				let data = {
					user_id: this.user_id
				};
				getUser(data).then(content => {
					// console.log(content);
					let {
						code,
						data
					} = content
					if (code == 200) {
						this.userInfo = data[0]
						this.username = data[0].user_nick_name == null ? '' : data[0].user_nick_name
						this.useremail = data[0].user_email == null ? '' : data[0].user_email;
						this.userphone = data[0].user_phone == null ? '' : data[0].user_phone;
						this.usermassage = data[0].user_message == null ? '' : data[0].user_message;
						// console.log(this.userInfo); 
					}
				})
			}
		},
		onLoad() {
			this.user_id = localStorage.getItem("user_id");
			this.user_avators = localStorage.getItem("user_avators")
			this.old_user_avators = localStorage.getItem("user_avators")

		},
		onShow() {
			this.getUserInfo();
		}
	}
</script>

<style lang="scss" scoped>
	.setting {
		width: 100%;
		padding: 0 30rpx;
	}

	.nav-header-box {
		// border: 1px solid #000;
		padding: 30rpx 0;
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: space-between;
		// height: 48rpx;

		.header-title {
			font-size: 32rpx;
			font-weight: bold;

		}

		.header-left,
		.header-right {
			width: 48rpx;
			height: 48rpx;
			background-repeat: no-repeat;
			background-size: 100%;
		}

		.header-left {
			background-image: url("@/static/image/detele.png");
		}

		.header-right {
			background-image: url("@/static/image/yes.png");
		}

	}

	.edit-img-box {
		position: relative;
		margin: 0 auto;
		width: 280rpx;
		height: 280rpx;

		image {
			border-radius: 50%;
			width: 100%;
			height: 100%;
		}

		.img-icon {
			z-index: 1;
			position: absolute;
			bottom: 10rpx;
			right: 20rpx;
			// width: 60rpx;
			// height: 60rpx;
			padding: 10rpx;
			border-radius: 50%;
			background-color: #1877F2;
			display: flex;
			align-items: center;

			.icon {
				// border-radius: 50%;
				// margin: 0 auto;
				width: 32rpx;
				height: 32rpx;
				// background-color: #fff;
				background-image: url("@/static/image/xj.png");
				background-repeat: no-repeat;
				background-size: 100%;
			}

		}
	}

	.edit-info-list {
		width: 100%;

		.edit-content {
			padding: 10rpx 0;

			.edit-title {
				font-size: 28rpx;
				color: #4E4B66;
				padding-bottom: 10rpx;
			}

			.edit-input {
				border-radius: 10rpx;
				border: 1px solid #4E4B66;
				padding: 20rpx 30rpx;
				box-sizing: content-box;
			}

			.edit-massage {
				width: 100%;
				height: 160rpx;
				border-radius: 10rpx;
				border: 1px solid #4E4B66;
				padding: 20rpx 30rpx;
				// box-sizing: content-box;
			}
		}
	}
</style>